import 'package:flutter/material.dart';

/// 从第二个页面返回数据给第一个页面
/// 1. 定义一个主页
/// 2. 在启动页面添加按钮
/// 3. 显示带两个按钮的页面
/// 4. 当按钮点击的时候关闭页面
/// 5. 在主页显示SnackBar

class ReturnDataApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    final String appTitle = 'Return Data Demo';
    return MaterialApp(
      title: appTitle,
      theme: ThemeData(primarySwatch: Colors.red),
      home: ReturnDataAppPage(title: appTitle),
    );
  }
}

class ReturnDataAppPage extends StatefulWidget {
  final String title;

  ReturnDataAppPage({Key key, this.title}) : super(key: key);

  @override
  State<StatefulWidget> createState() => ReturnDataAppPageState();
}

class ReturnDataAppPageState extends State<ReturnDataAppPage> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Center(
        child: SelectionButton(),
      ),
    );
  }
}

class SelectionButton extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return RaisedButton(
      child: Text('Pick an option, any option'),
      onPressed: () {
        _navigateAndDisplaySelection(context);
      },
    );
  }
}

_navigateAndDisplaySelection(BuildContext context) async {
  final result = await Navigator.push(
      context, MaterialPageRoute(builder: (context) => SelectionScreen()));
  Scaffold.of(context)
    ..removeCurrentSnackBar()
    ..showSnackBar(SnackBar(content: Text('$result')));
}

class SelectionScreen extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Pick an option'),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            Padding(
              padding: const EdgeInsets.all(8.0),
              child: RaisedButton(
                onPressed: () {
                  Navigator.pop(context, 'Yep!');
                },
                child: Text('Yep'),
              ),
            ),
            Padding(
              padding: const EdgeInsets.all(8.0),
              child: RaisedButton(
                onPressed: () {
                  Navigator.pop(context, 'Nope.');
                },
                child: Text('Nope'),
              ),
            )
          ],
        ),
      ),
    );
  }
}
